<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
<head>
  <title>文章信息</title>
  <t:base type="jquery,easyui,tools,DatePicker"></t:base>
</head>


<script type="text/javascript" src="${webRoot}/plug-in/ajaxfileupload/ajaxfileupload.js"></script>

<script type="text/javascript" charset="utf-8" src="${webRoot}/plug-in/ueditor1_4_3-utf8-jsp/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="${webRoot}/plug-in/ueditor1_4_3-utf8-jsp/ueditor.all.min.js"> </script>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<script type="text/javascript" charset="utf-8" src="${webRoot}/plug-in/ueditor1_4_3-utf8-jsp/lang/zh-cn/zh-cn.js"></script>



<script type="text/javascript">

  $(function() {
    $("#articleCategorySelect").combotree({
      onChange: function(n, o) {
        if($("#articleCategorySelect").combotree("getValues") != "") {
          $("#articleCategorySelect option").eq(1).attr("selected", true);
        } else {
          $("#articleCategorySelect option").eq(1).attr("selected", false);
        }
      }
    });
    <%--$("#orgSelect").combobox("setValues", ${orgIdList});--%>

//    $("#articleCategorySelect").combotree("setValues", []);
    $("#articleCategorySelect").combotree("setValues", ${articleCategoryList });

    f_initAttr();

  });

  //实例化编辑器
  //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
  var ue = UE.getEditor('editor');

  //添加内容改变事件,编辑器内容改变后把内容赋值到隐藏表单
  ue.addListener( 'contentChange', function( editor ) {

    $("#contents").val(ue.getContent());

  } );

  /**
   * 提交之前触发
   */
  function f_contents(){

    var desc = ue.getContent();

    //替换图片根路径
    desc = desc.replace(/src=\"\${webRoot}\/upload/g,'src="/upload');

    $("#contents").val(desc);

    //设置分类下拉框值
    var articleCategoryIds = $("#articleCategorySelect").combotree("getValues");
    $("#articleCategoryIds").val(articleCategoryIds);

  }

  //加载完富文本后放入内容
  ue.ready( function( editor ) {


    var html = '${article.contents}';

    html = html.replace(/src="\/upload/g,'src="${webRoot}/upload');

    ue.setContent(html);
  } );


  //Ajax文件上传
  function f_file_upload(fileInputId,valueId,formatId){

    //var result =/\.[^\.]+/.exec($("#fulAchievements").val());

    var path = $("#"+fileInputId).val();

    var pos1 = path.lastIndexOf("\\");
    var pos2 = path.lastIndexOf(".");
    var pos = path.substring(pos1 + 1, pos2);

    var fileName = path.substring(pos1+1,pos2);

    var str = $("#"+fileInputId).val().split(".");
    var result = path.substring(pos2 + 1,path.length );

//		 var rars = ".rar.zip.7z.cab.RAR.ZIP.7Z.CAB";
//
//		 var imgs = ".bmp.jpg.dib.jpeg.png.jfif.jpe.tif.tiff.gif.BMP.JPG.DIB.JPEG.PNG.JFIF.JPE.TIF.TIFF.GIF";
    var imgs = ".bmp.jpg.dib.jpeg.png.jfif.jpe.tif.tiff.gif.doc.docx.dotx.xls.xlsx.xltx.xlsb.ppt.pptx.ppsx.potx.rar.zip.7z.cab.txt";

    //文件类型 false为压缩包
    var boo = false;
    if(imgs.toLowerCase().indexOf(result.toLowerCase()) > -1){
      boo = true;

    }else{

      alert(" 只能上传后缀为 .bmp.jpg.dib.jpeg.png.jfif.jpe.tif.tiff.gif 的图片" +
      "或者后缀为.rar.zip.7z.cab的压缩文件" +
      "或者后缀为.doc.docx.dotx.xls.xlsx.xltx.xlsb.ppt.pptx.ppsx.potx.txt的office文件！");
      $("#"+fileInputId).val("");
      return;
    }


    $.ajaxFileUpload({
      url: '${webRoot}/myFileUpload/upload_resource_file.do',
      secureuri: false,
      fileElementId: fileInputId,
      //dataType: 'json',
      success: function (data) {
        //data = $.parseJSON(data);

        var data2 = data.split(",");

        var url = data2[0].replace(" ","");
        fileName = data2[1].replace(" ","");


        f_getAttachImg(result,url,fileName);
        $("#"+valueId).val(url);
        $("#"+formatId).val(result);

        $("#downloadFile").attr("href","${webRoot}/fileDownload/download_file.do?res_url="+url);

      },
      error:function(data){

        console.log("error");
        console.log(data);
      }
    });
  }

  //显示文件图标
  function f_getAttachImg(suffix,path,fileName){


    var imgs = ".bmp.jpg.dib.jpeg.png.jfif.jpe.tif.tiff.gif";

    var rar = ".rar.zip.7z.cab";

    var office = ".doc.docx.dotx.xls.xlsx.xltx.xlsb.ppt.pptx.ppsx.potx.pdf";

    var txt = ".txt";

    var imgSrc = "";
    if(imgs.toLowerCase().indexOf(suffix.toLowerCase()) > -1){

      imgSrc = "${webRoot}/"+path;

    }else if(rar.toLowerCase().indexOf(suffix.toLowerCase()) > -1){

      imgSrc = "${webRoot}/upload/system/imgs/yasuobao.png";
    }else if(office.toLowerCase().indexOf(suffix.toLowerCase()) > -1){

      imgSrc = "${webRoot}/upload/system/imgs/office.png";
    }else if(txt.toLowerCase().indexOf(suffix.toLowerCase()) > -1){
      imgSrc = "${webRoot}/upload/system/imgs/txt.png";
    }

    $("#suffixImg").attr("src",imgSrc);
    $("#suffixImg").attr("alt",fileName);
    $("#suffixImg").attr("title",fileName);

  }

  //初始化附件
  function f_initAttr(){

    var path = "${article.tAttachment.path}";


    if(path == ""){
      return;
    }

    var pos1 = path.lastIndexOf("\\");
    var pos2 = path.lastIndexOf(".");

    var fileName = path.substring(pos1+1,pos2);

    var result = path.substring(pos2 + 1,path.length );

    f_getAttachImg(result,path,fileName);

  }

</script>

<body style="overflow-y: auto" scroll="no">
<t:formvalid formid="formobj" beforeSubmit="f_contents()" dialog="true" usePlugin="password" layout="table" action="articleController.do?save">
  <input id="id" name="id" type="hidden" value="${article.id }">
  <table style="width: 750px;" cellpadding="0" cellspacing="1" class="formtable">
    <tr>
      <td align="right">
        <label class="Validform_label">
          内容标题:
        </label>
      </td>
      <td class="value">
        <input class="inputxt" id="title" name="title"
               value="${article.title}" datatype="*">
        <span class="Validform_checktip"></span>
      </td>
      <td align="right">
        <label class="Validform_label">
          内容副标题:
        </label>
      </td>
      <td class="value">
        <input class="inputxt" id="subtitle" name="subtitle"
               value="${article.subtitle}" >
        <span class="Validform_checktip"></span>
      </td>
    </tr>

    <tr>
      <td align="right"><label class="Validform_label"> 文章分类: </label></td>
      <td class="value">
        <select class="easyui-combotree" data-options="url:'articleCategoryController.do?setPFunction', multiple:true, cascadeCheck:true"
                id="articleCategorySelect" name="articleCategorySelect">
            <%--datatype="select1"--%>

          <%--<c:forEach items="${goodsCategoryList}" var="goodsCategory">--%>
            <%--<option value="${goodsCategory.id }">${goodsCategory.title}</option>--%>
          <%--</c:forEach>--%>
        </select>
        <input id="articleCategoryIds" name="articleCategoryIds" type="hidden" >
        <span class="Validform_checktip">请选择文章分类</span>
      </td>

      <td align="right">
        <label class="Validform_label">
          是否发布:
        </label>
      </td>
      <td class="value">

        <select name="isPublish" id="ispublish">

          <option value="1" <c:if test="${article.isPublish>0}"> selected="selected"</c:if>>
            是
          </option>
          <option value="0" <c:if test="${article.isPublish eq 0}">selected="selected"</c:if>>
            否
          </option>
        </select>

        <span class="Validform_checktip"></span>
      </td>
    </tr>

    <tr>
      <td align="right">
        <label class="Validform_label">
          发布平台:
        </label>
      </td>
      <td class="value">
        <t:dictSelect field="publishPlat"  type="checkbox" typeGroupCode="supportSys" defaultVal="${article.publishPlat}"
                      hasLabel="false" extendJson="{datatype='*'}" >
        </t:dictSelect>
        <span class="Validform_checktip"></span>
      </td>

    </tr>

    <tr>
      <td align="right">
        <label class="Validform_label">
          附件:
        </label>
      </td>
      <td class="value">

        <img id="suffixImg" src="" style="width: 100px;height: 100px;" alt="预览" title="预览">

        <c:if test="${article.tAttachment != '' && article.tAttachment != null}">
          <a id="downloadFile" href="${webRoot}/fileDownload/download_file.do?res_url=${article.tAttachment.path}">下载附件</a>
        </c:if>

        <input type="button" name="" onclick="javascript:$(this).next().trigger('click')" class="uploadify-button" value="上传" />
        <input  type="file" style="display: none;" name="file" id="fulAchievements1" onchange="f_file_upload('fulAchievements1','path','format')" />
        <input type="hidden" name="tAttachment.path" id="path" value="${article.tAttachment.path}"/>
        <input type="hidden" name="tAttachment.format" id="format" value="${article.tAttachment.format}"/>
        <input type="hidden" name="tAttachment.attachmentId" id="attachmentId" value="${article.tAttachment.attachmentId}"/>

        <span class="Validform_checktip"></span>
      </td>
    </tr>

    <tr>
      <td align="right">
        <label class="Validform_label">
          内容:
        </label>
      </td>
      <td class="value" colspan="3">

        <script id="editor" type="text/plain" style="width:100%;height:300px;"></script>
        <textarea style="display: none;"  id="contents" name="contents" datatype="*">${article.contents}</textarea>
        <span class="Validform_checktip"></span>
      </td>
    </tr>
  </table>
</t:formvalid>
</body>